<?php
class User extends CI_Controller {
    public function __construct() {
        parent::__construct();
        $this -> output -> enable_profiler(PROFILER);
        is_logged_in();
        $this -> em = $this -> doctrine -> em;
    }    public function index() {
        $this -> load -> library('table');
        if ($this -> input -> post('search')) {
            if (array_key_exists('search', $_POST)) {
                $term = $this -> input -> post('search');
                $foundUsers = $this -> em -> getRepository('models\User') -> findBy(array('username' => $term));
            }
            if (count($foundUsers) > 0) {                $data['users'] = $foundUsers;            }
        } else {
            if (PAGINATION_ENABLED) {
                $totalOfRows = count($this -> em -> getRepository('models\User') -> findAll());                $this -> load -> library('pagination');                $config['base_url'] = site_url() . '/user/index';
                $config['total_rows'] = $totalOfRows;
                $this -> pagination -> initialize($config);
                if ($this -> uri -> segment(3)) {
                    $offset = $this -> uri -> segment(3);
                } else {
                    $offset = 0;
                }                $users = $this -> em -> getRepository('models\User') -> findBy(array(), array(), $this -> pagination -> per_page, $offset);
            } else {                $users = $this -> em -> getRepository('models\User') -> findAll();            }            if ($users) {                $data['users'] = $users;            }        }
        $data['main_content'] = "admin/user/index";
        $this -> load -> view("admin/template", $data);
    }    public function create() {
        if ($this -> form_validation -> run('user/create') == TRUE) {
            try {                $user = new models\User();                models\User::prepareData($_POST, $user);                $this -> em -> persist($user);                $this -> em -> flush();
                $this -> session -> set_flashdata('message', array('title' => 'User Created', 'content' => 'The user has been sucessfully added', 'type' => ALERT_TYPE_SUCCESS));
                redirect('user/index');
            } catch(Exception $e) {
                $this -> session -> set_flashdata('message', array('title' => 'User Created', 'content' => 'A database Error has occur, please contact your administrator', 'type' => ALERT_TYPE_ERROR));
                redirect('user/index');
            }
        } else {            $data['user'] = new models\User();
            $data['main_content'] = "admin/user/form";
            $this -> load -> view("admin/template", $data);
        }
    }    public function edit($id) {
        $id = (int)$id;
        $data['id'] = $id;
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {            if ($this -> form_validation -> run("user/edit") == TRUE) {
                try {                    $user = $this -> doctrine -> em -> find('models\User', $id);                    models\User::prepareData($_POST, $user);                    $this -> em -> persist($user);                    $this -> em -> flush();
                    $this -> session -> set_flashdata('message', array('title' => 'User Updated', 'content' => 'The user has been sucessfully updated', 'type' => ALERT_TYPE_SUCCESS));
                    redirect('user/index');
                } catch (Exception $e) {
                    $this -> session -> set_flashdata('message', array('title' => 'User Not Updated', 'content' => 'A database Error has occur, please contact your administrator', 'type' => ALERT_TYPE_ERROR));
                    redirect('user/index');
                }
            } else {
                $data['user'] = new models\User();
                $data['main_content'] = "admin/user/form";
                $this -> load -> view("admin/template", $data);
            }
        } else {
            $data['user'] = $this -> doctrine -> em -> find('models\User', $id);
            if ($data['user']) {
                $data['main_content'] = "admin/user/form";
                $this -> load -> view("admin/template", $data);
            } else {
                $this -> session -> set_flashdata('message', array('title' => 'Not able to retrieve user registry', 'content' => 'A database Error has occur, please contact your administrator', 'type' => ALERT_TYPE_WARNING));
                redirect('user/index');
            }
        }
    }    public function delete($id) {         try {            $user = $this -> doctrine -> em -> find('models\User', $id);            $this -> em -> remove($user);            $this -> em -> flush();              $this -> session -> set_flashdata('message', array('title' => 'User deleted', 'content' => 'The user has been sucessfully deleted', 'type' => ALERT_TYPE_SUCCESS));        }                 catch(Exception $e) {            $this -> session -> set_flashdata('message', array('title' => 'Not able to retrieve user registry', 'content' => 'A database Error has occur, please contact your administrator', 'type' => ALERT_TYPE_WARNING));        }        redirect('user/index');                
    }            }?>